
const int chips_size = 20;
const uint32_t chips[chips_size] = {
  2716, 2732,
  2764, 27128, 27256, 27512,
  27010, 27020, 27040, 27080,
  2864, 28256,
  29010, 29020, 29040,
  29011, 29021, 29041,
         29022, 29042
};

//判断是否支持
void checkChip(uint32_t chipId) {
  for (int i = 0; i < chips_size; i++) {
    if (chipId == chips[i]) {
      Serial.println(F("ok"));
      return;
    }
  }
  Serial.println("Not support this chip:" + chipId);
}

void read_mode(void) {
//  pinMode(Data_0, INPUT_PULLUP);
//  pinMode(Data_1, INPUT_PULLUP);
//  pinMode(Data_2, INPUT_PULLUP);
//  pinMode(Data_3, INPUT_PULLUP);
//  pinMode(Data_4, INPUT_PULLUP);
//  pinMode(Data_5, INPUT_PULLUP);
//  pinMode(Data_6, INPUT_PULLUP);
//  pinMode(Data_7, INPUT_PULLUP);
  portMode(3, INPUT_PULLUP);
  //  DDRD = 0x00;
}


void write_mode(void) {
  //  pinMode(Data_0, OUTPUT);
  //  pinMode(Data_1, OUTPUT);
  //  pinMode(Data_2, OUTPUT);
  //  pinMode(Data_3, OUTPUT);
  //  pinMode(Data_4, OUTPUT);
  //  pinMode(Data_5, OUTPUT);
  //  pinMode(Data_6, OUTPUT);
  //  pinMode(Data_7, OUTPUT);
  portMode(3, OUTPUT);
  //  DDRD = 0xff;
}

// 读取数据
byte get_data(void) {
  return portRead(3) & 0xff;
  //  return PIND;
}

// inline同步限制
void set_data(byte data) {
  //  portWrite(3,data);
  portWrite(3, data);
  //  PORTD = data & 0xff;
}








// 命令最大值+2
void readCommand() {
  for (uint8_t i = 0; i < 22; i++) {
    cmdBuffer[i] = 0; // clear command buffer
  }
  char c; uint8_t idx = 0;                        // initialize variables
  do {
    if (Serial.available()) {
      c = Serial.read();
      cmdBuffer[idx++] = c;
    }
  } while (c != '\n' && idx < 21);
  cmdBuffer[idx - 1] = 0;                     //把最后一位换行符改为 '\0' termination
}


// converts character representing a hex nibble into 4-bit value
uint8_t hexDigit(char c) {
  if      (c >= '0' && c <= '9') return c - '0';
  else if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  else if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  else return 0;
}


//将字符串数据转为byte。如 FF改为 255
byte hexByte(char* a) {
  return ((hexDigit(a[0]) * 16) + hexDigit(a[1]));
}

// 返回字芯片字符串
uint32_t hexChip(char* data) {
  uint32_t re = 0;
  if (data[0] >= 0x30 && data[0] <= 0x39)re += (data[0] - 0x30) * 100000;
  if (data[1] >= 0x30 && data[1] <= 0x39)re += (data[1] - 0x30) * 10000;
  if (data[2] >= 0x30 && data[2] <= 0x39)re += (data[2] - 0x30) * 1000;
  if (data[3] >= 0x30 && data[3] <= 0x39)re += (data[3] - 0x30) * 100;
  if (data[4] >= 0x30 && data[4] <= 0x39)re += (data[4] - 0x30) * 10;
  if (data[5] >= 0x30 && data[5] <= 0x39)re += (data[5] - 0x30) * 1;
  return re;
}

//将字符串转为10进制
uint32_t hexWord(char* data) {
  return (
           (hexDigit(data[0]) * 4096) +
           (hexDigit(data[1]) * 256) +
           (hexDigit(data[2]) * 16) +
           (hexDigit(data[3])));
}
